/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024.
 * Description: fast svc support only for rtos
 * Author: liwugui <liwugui1@huawei.com>
 * Create: 2024-04-20
 */

#include <linux/errno.h>
#include <linux/spinlock.h>
#include <linux/module.h>
#include "rtos_fast_svc.h"

int rtos_fast_svc_handler_ctx_default(unsigned long arg0)
{
	return 0;
}

static DEFINE_SPINLOCK(rtos_fast_svc_lock);

rtos_fast_svc_handler_ctx_t rtos_fast_svc_handler_ctx = rtos_fast_svc_handler_ctx_default;

int register_rtos_fast_svc_handler_ctx(rtos_fast_svc_handler_ctx_t func)
{
	if (func == NULL) {
		pr_err("rtos_fast_svc_handler_ctx can not be NULL\n");
		return -EINVAL;
	}
	if (spin_trylock(&rtos_fast_svc_lock)) {
		rtos_fast_svc_handler_ctx = func;
		pr_info("rtos_fast_svc_handler_ctx is registered successfully\n");
		spin_unlock(&rtos_fast_svc_lock);
		return 0;
	}
	return -EBUSY;
}
EXPORT_SYMBOL_NS(register_rtos_fast_svc_handler_ctx, HW_RTOS_NS);

int unregister_rtos_fast_svc_handler_ctx(void)
{
	if (spin_trylock(&rtos_fast_svc_lock)) {
		if (rtos_fast_svc_handler_ctx != rtos_fast_svc_handler_ctx_default)
			rtos_fast_svc_handler_ctx = rtos_fast_svc_handler_ctx_default;
		else
			pr_err("rtos_fast_svc_handler_ctx is not registered\n");
		spin_unlock(&rtos_fast_svc_lock);
		return 0;
	}
	return -EBUSY;
}
EXPORT_SYMBOL_NS(unregister_rtos_fast_svc_handler_ctx, HW_RTOS_NS);
